package com.qf.smsplatfrom.controller;

import com.github.pagehelper.PageInfo;
import com.qf.smsplatfrom.dto.R;
import com.qf.smsplatfrom.pojo.DirtyWord;
import com.qf.smsplatfrom.pojo.TAdminUser;
import com.qf.smsplatfrom.service.DirtyWordService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dirtyword")

public class DirtyWordController {
    @Autowired
    private DirtyWordService dirtyWordService;
    @GetMapping("/dirtyword/{id}")
    public R showById(@PathVariable Long id){
        DirtyWord word = dirtyWordService.showById(id);
        R r = R.setOk(word);
        return r;
    }
    @PostMapping("/add/{dirtyword}")
    public R addDirtyWord(@PathVariable String dirtyword, HttpSession session){
        DirtyWord dirtyWord = new DirtyWord();
        dirtyWord.setDirtyword(dirtyword);
        Object user = session.getAttribute("user");
        if(user!=null){
            long id = ((TAdminUser) user).getId();
            long type = ((TAdminUser) user).getType();
            dirtyWord.setOwntype(type);
            dirtyWord.setCreater(id);
        }
        dirtyWordService.addDirtyWord(dirtyWord);
        return R.setOk();
    }
    @GetMapping("/showAll")
    public R showAll(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<DirtyWord> pageInfo = dirtyWordService.showAll(pageNum, pageSize);
        return R.setOk(pageInfo);
    }
    @PutMapping("/dirtyword")
    public R update(@RequestBody DirtyWord dirtyWord,HttpSession session){
        Object user = session.getAttribute("user");
        if(user!=null){
            long id = ((TAdminUser) user).getId();
            long type = ((TAdminUser) user).getType();
            dirtyWord.setCreater(id);
            dirtyWord.setOwntype(type);
        }
        dirtyWordService.updateDirtyById(dirtyWord);
        return R.setOk();
    }
    @DeleteMapping("/dirtyword")
    public R delete(@RequestBody Map map){
        dirtyWordService.deleteDirtyById(((List<Integer>) map.get("ids")));
        return R.setOk();
    }
}
